#pragma once
#include "Connection.hpp"
#include "Log.hpp"
#include "SetNonBlock.hpp"
#include "HandlerConnection.hpp"

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <functional>

class Accepter
{
public:
    // void AccepterConntion(Connection* conn)
    // {
    //     while(true)
    //     {
    //         struct sockaddr_in peer; //<netinet/in.h>
    //         socklen_t len = sizeof peer;
    //         int sockfd = accept(conn->GetSockfd(), (sockaddr*)&peer, &len);
    //         if(sockfd > 0)
    //         {
    //             lg.LogMessage(Info, "accept success..., sockfd: %d\n", sockfd);
    //             SetNonBlock(sockfd);           //&&&&复习bind：bind顺序，还有&&
    //             func_t recver = std::bind(&HandlerConnection::Recver, std::placeholders::_1);
    //             func_t sender = std::bind(&HandlerConnection::Sender, std::placeholders::_1);
    //             func_t excepter = std::bind(&HandlerConnection::Excepter, std::placeholders::_1);
    //             Connection* normal_conn = ConnectionFactory::BulidNormalConnection(sockfd, 
    //                 recver, sender, excepter, EPOLLIN|EPOLLET, conn->_R);
                
    //             normal_conn->_R->AddConnetion(normal_conn);
    //         }
    //         else
    //         {
    //             if(errno == EAGAIN)
    //                 break;
    //             else if(errno == EINTR)
    //                 continue;
    //             else
    //             {
    //                 lg.LogMessage(Warning, "get a new link error\n");
    //                 break;
    //             }
    //         }
    //     }
    // }
    // Accepter.hpp
void AccepterConntion(Connection* conn)
{
    while (true)
    {
        struct sockaddr_in peer; //<netinet/in.h>
        socklen_t len = sizeof peer;
        int sockfd = accept(conn->GetSockfd(), (sockaddr*)&peer, &len);
        if (sockfd > 0)
        {
            lg.LogMessage(Info, "Accept success..., sockfd: %d\n", sockfd);
            SetNonBlock(sockfd);  // 设置非阻塞

            // 创建回调函数
            func_t recver = std::bind(&HandlerConnection::Recver, std::placeholders::_1);
            func_t sender = std::bind(&HandlerConnection::Sender, std::placeholders::_1);
            func_t excepter = std::bind(&HandlerConnection::Excepter, std::placeholders::_1);

            // 创建连接
            Connection* normal_conn = ConnectionFactory::BulidNormalConnection(sockfd, 
                recver, sender, excepter, EPOLLIN|EPOLLET, conn->_R);
            
            // 添加连接
            normal_conn->_R->AddConnetion(normal_conn);
        }
        else
        {
            if (errno == EAGAIN)
            {
                lg.LogMessage(Warning, "No more connections available\n");
                break;
            }
            else if (errno == EINTR)
            {
                continue;
            }
            else
            {
                lg.LogMessage(Warning, "Error accepting connection: %s\n", strerror(errno));
                break;
            }
        }
    }
}

};